package com.demo.juc.order;


import java.util.Date;

/**
 * @Author: shubin_yao
 * @Date: 2022/04/30/18:05
 * @Description: 数据模型：
 * A,B,C 三个线程实现
 * A-> B -> C
 * A 打印 5 次，B 打印10 次，C 打印15次 顺序执行，来2轮
 * <p>
 * 实现方案：
 * 采用新版Lock-condition 设置标志位，精准唤醒
 * <p>
 * 实际业务场景 ：下订单--> 减库存 --》调支付
 * <p>
 * 顺序执行方案：
 * (1) 非阻塞模型 ：采用新版Lock-condition 设置标志位，精准唤醒
 * (2) 线程阻塞模型 ：采用新版join让主线程等待子线程执行完成
 */
public class ThreadOrderAccess {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
     /*   ShareResource shareResource = new ShareResource();
        new Thread(() -> {
            for (int i = 0; i < 2; i++) {
                try {
                    shareResource.print5();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 2; i++) {
                try {
                    shareResource.print10();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 2; i++) {
                try {
                    shareResource.print15();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/

        ShareJoin shareJoin = new ShareJoin();
        for (int i = 0; i < 2; i++) {
            Thread t1 = new Thread(() -> {
                try {
                    shareJoin.print5();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "A");
            Thread t2 = new Thread(() -> {
                try {
                    shareJoin.print10();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "B");
            Thread t3 = new Thread(() -> {
                try {
                    shareJoin.print15();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "C");

            t1.start();
            t1.join();

            t2.start();
            t2.join();

            t3.start();
            t3.join();

        }

        long end = System.currentTimeMillis();
        System.out.println("总用时：" + (end - start) + "ms");
    }

}
